package com.github.fujianlian.klinechart.draw;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.R;
import com.github.fujianlian.klinechart.base.IValueFormatter;
import com.github.fujianlian.klinechart.entity.h.IDoubleCCI;
import com.github.fujianlian.klinechart.formatter.ValueFormatter;
import com.github.fujianlian.klinechart.utils.ViewUtil;

import java.util.List;

/**
 * KDJ实现类
 * Created by tifezh on 2016/6/19.
 */

public class CCIMNDraw extends BaseDraw<IDoubleCCI,IDoubleCCI> {
    private Paint mNCCIPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mMCCIPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    float lineTop = 100;
    float lineBottom = -100;

    public CCIMNDraw() {
        super(IDoubleCCI.class);

    }

    public CCIMNDraw(BaseKLineChartView view, float lineTop, float lineBottom) {
        this(view);
        this.lineTop = lineTop;
        this.lineBottom = lineBottom;
    }

    public CCIMNDraw(BaseKLineChartView view) {
        super(IDoubleCCI.class);

        init(view);
    }

    @Override
    public void init(BaseKLineChartView view) {
        mNCCIPaint.setColor(ContextCompat.getColor(view.getContext(), R.color.colorAccent));
        mMCCIPaint.setColor(ContextCompat.getColor(view.getContext(), R.color.color_2a5caa));
        mNCCIPaint.setTextSize(ViewUtil.Dp2Px(view.getContext(), 10));
        mMCCIPaint.setTextSize(ViewUtil.Dp2Px(view.getContext(), 10));


        mLine1Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_red));
        mLine2Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_green));

        mLine1Paint.setStrokeWidth(2);
        mLine2Paint.setStrokeWidth(2);
    }


    public float getLineTop() {
        return lineTop;
    }

    public void setLineTop(float lineTop) {
        this.lineTop = lineTop;
    }

    public float getLineBottom() {
        return lineBottom;
    }

    public void setLineBottom(float lineBottom) {
        this.lineBottom = lineBottom;
    }


    @Override
    public void drawTranslated(@Nullable IDoubleCCI lastPoint, @NonNull IDoubleCCI curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {

    }

    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        IDoubleCCI point = (IDoubleCCI) view.getItem(position);
        String text = "CCI(14,54)  ";
        canvas.drawText(text, x, y, view.getTextPaint());
        x += view.getTextPaint().measureText(text);
        text = "" + view.formatValue(point.getNCCI()) + "," + view.formatValue(point.getMCCI());
        canvas.drawText(text, x, y, mNCCIPaint);
    }

    @Override
    public Float getMaxValue(IDoubleCCI point) {
        return Math.max(point.getNCCI(), point.getMCCI());
    }

    @Override
    public float getMinValue(IDoubleCCI point) {
        return Math.min(point.getNCCI(), point.getMCCI());

    }

    @Override
    public IValueFormatter getValueFormatter() {
        return new ValueFormatter();
    }

    @Override
    public void drawTranslateds(Canvas canvas, BaseKLineChartView view, List<IDoubleCCI> datas, int mStartIndex, int mStopIndex) {

        Rect chartRect = view.getChartRect(this);

        float startX = view.getX(mStartIndex);
        float endX = view.getX(mStopIndex);

        float volY1 = view.getVolY(this, lineTop);
        if (volY1 >= chartRect.top) {
            canvas.drawLine(startX, volY1, endX, volY1, mLine1Paint);
        }

        float volY = view.getVolY(this, lineBottom);
        if (volY <= chartRect.bottom) {
            canvas.drawLine(startX, volY, endX, volY, mLine2Paint);
        }

        draw(canvas, view, mStartIndex, mStopIndex, true);
        draw(canvas, view, mStartIndex, mStopIndex, false);

    }

    private void draw(Canvas canvas, BaseKLineChartView view, int mStartIndex, int mStopIndex, boolean isN) {
        float[] pts = new float[(mStopIndex - mStartIndex + 1) * 4];

        for (int i = mStartIndex; i <= mStopIndex; i++) {
            IDoubleCCI currentPoint = (IDoubleCCI) view.getItem(i);
            float currentPointX = view.getX(i);

            IDoubleCCI lastPoint = i == 0 ? currentPoint : (IDoubleCCI) view.getItem(i - 1);
            float lastX = i == 0 ? currentPointX : view.getX(i - 1);

            pts[(i - mStartIndex) * 4] = lastX;
            pts[(i - mStartIndex) * 4 + 1] = view.getVolY(this, isN ? lastPoint.getNCCI() : lastPoint.getMCCI());
            pts[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts[(i - mStartIndex) * 4 + 3] = view.getVolY(this, isN ? currentPoint.getNCCI() : currentPoint.getMCCI());

        }

        view.drawLines(canvas, isN ? mNCCIPaint : mMCCIPaint, pts);
    }


    /**
     * 设置曲线宽度
     */
    public void setLineWidth(float width) {
        mNCCIPaint.setStrokeWidth(width);
        mMCCIPaint.setStrokeWidth(width);

    }

    /**
     * 设置文字大小
     */
    public void setTextSize(float textSize) {
        mNCCIPaint.setTextSize(textSize);
        mMCCIPaint.setTextSize(textSize);
    }
}
